#include <am.h>
#include <riscv.h>
#include <npc.h>
#define SYNC_ADDR (VGACTL_ADDR + 4)
int w=0;
void __am_gpu_init() {
  w=(uint32_t)inw(VGACTL_ADDR+2);
}

void __am_gpu_config(AM_GPU_CONFIG_T *cfg) {
    int w,h;
    w=(uint32_t)inw(VGACTL_ADDR+2);
    h=(uint32_t)inw(VGACTL_ADDR);
    *cfg = (AM_GPU_CONFIG_T) {
    .present = true, .has_accel = false,
    .width = w, .height = h,
    .vmemsz = 0
  };
  cfg->vmemsz=cfg->width*cfg->height*sizeof(uint32_t);
}

void __am_gpu_fbdraw(AM_GPU_FBDRAW_T *ctl) {
  const uint32_t *data=ctl->pixels;
    // int w;
    // w=(uint32_t)inw(VGACTL_ADDR+2);
    // h=(uint32_t)inw(VGACTL_ADDR);

  for(size_t i=0;i<ctl->h;i++){
    for(int j=0;j<ctl->w;j++){
      outl(((400*(ctl->y+i)+(ctl->x+j))<<2)+FB_ADDR,*(data++));
    }
  }
  
  
  if (ctl->sync) {
    outl(SYNC_ADDR, 1);
  }
}

void __am_gpu_status(AM_GPU_STATUS_T *status) {
  status->ready = true;
}
